package com.lijing.controller;

import com.lijing.model.Activity;
import com.lijing.model.User;
import com.lijing.service.ActivityService;
import com.lijing.service.UserService;
import com.lijing.util.JSONResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
public class ActivityController {

    @Autowired
    private ActivityService activityService;

    @Autowired
    private UserService userService;

    @RequestMapping("/selectActivityByClubWidthMember")
    public List<Activity> selectActivityByClubWidthMember(Integer cid, HttpSession session){
        User user = (User) session.getAttribute("user");
        List<Activity> activities = null;
        activities = activityService.selectActivityByClubWidthMember(cid);
        for (Activity activity:activities){
            activity.setJoin(activityService.isJoin(activity.getAid(), user.getUid()));
            activity.setMember_count(activityService.getMemberCount(activity));
        }
        return activities;
    }

    @RequestMapping("/selectActivityByClub")
    public List<Activity> selectActivityByClub(Integer cid, HttpSession session){
        User user = (User) session.getAttribute("user");
        List<Activity> activities = null;
        activities = activityService.selectActivityByClub(cid);
        for (Activity activity:activities){
            activity.setJoin(activityService.isJoin(activity.getAid(), user.getUid()));
            activity.setMember_count(activityService.getMemberCount(activity));
        }
        return activities;
    }

    @RequestMapping("/selectActivityByClubAuth")
    public List<Activity> selectActivityByClubAuth(Integer cid, Integer minAuth, Integer maxAuth, HttpSession session){
        User user = (User) session.getAttribute("user");
        List<Activity> activities = null;
        activities = activityService.selectActivityByClubAuth(cid, minAuth, maxAuth);
        for (Activity activity:activities){
            activity.setMember_count(activityService.getMemberCount(activity));
        }
        return activities;
    }

    @RequestMapping("/selectActivityByAuth")
    public List<Activity> selectActivityByAuth(Integer minAuth, Integer maxAuth, HttpSession session){
        User user = (User) session.getAttribute("user");
        List<Activity> activities = null;
        activities = activityService.selectActivityByAuth(minAuth, maxAuth);
        for (Activity activity:activities){
            activity.setMember_count(activityService.getMemberCount(activity));
        }
        return activities;
    }

    @RequestMapping("/getActivity")
    public Activity getActivity(Integer aid, HttpSession session){
        User user = (User) session.getAttribute("user");
        Activity activity = activityService.getActivity(aid);
        activity.setJoin(activityService.isJoin(activity.getAid(), user.getUid()));
        return activity;
    }

    @RequestMapping(value = "/updateActivity", produces = "application/json")
    public String updateActivity(Activity activity){
        return JSONResponse.getOK("更新成功！");
    }

    @RequestMapping(value = "/joinActivityById",produces = "application/json;charset=UTF-8")
    public String joinActivity(Integer aid, Integer uid){
        activityService.joinActivity(aid, uid);
        return JSONResponse.getOK("报名成功！");
    }

    @RequestMapping(value = "/joinActivity",produces = "application/json;charset=UTF-8")
    public String joinActivity(Integer aid, HttpSession session){
        User user = (User)session.getAttribute("user");
        activityService.joinActivity(aid, user.getUid());
        return JSONResponse.getOK("报名成功！");
    }

    @RequestMapping(value = "/exitActivity",produces = "application/json;charset=UTF-8")
    public String exitActivity(Integer aid, HttpSession session){
        User user = (User)session.getAttribute("user");
        activityService.exitActivity(aid, user.getUid());
        return JSONResponse.getOK("取消报名成功！");
    }

    @RequestMapping(value = "/isJoin")
    public String isJoin(Integer aid, HttpSession session){
        User user = (User)session.getAttribute("user");
        return activityService.isJoin(aid, user.getUid()).toString();
    }

    @RequestMapping(value = "/deleteActivity", produces = "application/json;charset=UTF-8")
    public String deleteActivity(Integer[] aids){
        for (Integer aid:aids){
            activityService.deleteActivity(aid);
        }
        return JSONResponse.getOK("删除成功");
    }

    @RequestMapping(value = "/passActivity", produces = "application/json;charset=UTF-8")
    public String passActivity(Integer[] aids){
        Activity activity = null;
        for (Integer aid:aids){
            activity = activityService.getActivity(aid);
            activity.setStatus(1);
            activityService.updateActivity(activity);
        }
        return JSONResponse.getOK("修改活动状态成功！");
    }

    @RequestMapping(value = "/rejectActivity", produces = "application/json;charset=UTF-8")
    public String rejectActivity(Integer[] aids, String cause){
        Activity activity = null;
        for (Integer aid:aids){
            activity = activityService.getActivity(aid);
            activity.setStatus(-1);
            activityService.updateActivity(activity);
        }
        System.out.println("拒绝原因：" + cause);
        return JSONResponse.getOK("已拒绝！");
    }

    @RequestMapping(value = "/addActivity", produces = "application/json;charset=UTF-8")
    public String addActivity(Activity activity, HttpSession session){
        User user = (User)session.getAttribute("user");
        if ("社长".equals(user.getType())){
            activity.setInitiator(user);
            activity.setAclub(user.getClub());
            activity.setStatus(0);
            activityService.addActivity(activity);
            return JSONResponse.getOK("提交申请成功！请耐心等待审核");
        }else {
            return JSONResponse.getError(-1,"您没有权限！");
        }
    }
    @RequestMapping(value = "/selectActivityByUser")
    public List<Activity> selectActivityByUser(Integer uid, String aname, HttpSession session){
        User user = (User)session.getAttribute("user");
        List<Activity> activities = activityService.selectActivityByUser(uid, aname);
        return activities;
    }

    @RequestMapping(value = "/selectActivityByDate")
    public List<Activity> selectActivityByUser(Integer uid, String earliest, String latest, HttpSession session){
        User user = (User)session.getAttribute("user");
        List<Activity> activities = activityService.selectActivityByDate(uid, earliest, latest);
        return activities;
    }
}
